layui.define(["jquery"], function (exports) {
    let $ = layui.jquery;
    let layplugin = {
        serializeObject: function ($dom) {
            let o = {};
            let a = $dom.serializeArray();
            $.each(a, function () {
                if (o[this.name]) {
                    if (!o[this.name].push) {
                        o[this.name] = [o[this.name]];
                    }
                    o[this.name].push(this.value.trim() || '');
                } else {
                    o[this.name] = this.value.trim() || '';
                }
            });
            return o;
        },
        addOptionsWithKeyValue: function ($dom, keyValuePairs, clear) {
            if (clear) {
                $(this).html('');
            }
            for (let key in keyValuePairs) {
                $(this).append(
                    '<option value="' + keyValuePairs[key].id + '">' + keyValuePairs[key].name + '</option>'
                );
            }
        },
        chart:{
            showLoading: function (echarts, elementId) {
                let element = document.getElementById(elementId);
                let chart = echarts.init(element);
                chart.showLoading({
                    text: "图表数据正在努力加载..."
                });
            },
            showNoData: function (echarts, elementId) {
                let element = document.getElementById(elementId);
                let chart = echarts.init(element);
                let msgOption = {
                    title: {
                        show: true,
                        textStyle: {
                            color: 'grey',
                            fontSize: 20
                        },
                        text: '暂无数据',
                        left: 'center',
                        top: 'center'
                    },
                    xAxis: {
                        show: false
                    },
                    yAxis: {
                        show: false
                    },
                    series: []
                };
                chart.setOption(msgOption, true);
                chart.hideLoading();
            },
            hideLoading: function (echarts, elementId) {
                let element = document.getElementById(elementId);
                let chart = echarts.init(element);
                chart.hideLoading()
            },
            showChart: function (echarts, elementId, option) {
                let element = document.getElementById(elementId);
                let chart = echarts.init(element);
                chart.setOption(option, true);
                chart.hideLoading();
            },
            initPieOption: function (echarts, obj, unit) {
                if (!unit) {
                    unit = ''
                }
                return {
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/> {b}" + unit + " ({d}%)"
                    },
                    color: [
                        "#1792E5",
                        "#7EB338",
                        "#FFC627",
                        "#F37021",
                        "#EE3124",
                        "#9B5986",
                        "#2a3154",
                        "#e97ccb"
                    ],
                    legend: {
                        type: 'scroll',
                        orient: 'vertical',
                        left: 'right',
                        bottom: 'center',
                        itemWidth: 10,
                        itemHeight: 10,
                        right: 30,
                        textStyle: {
                            fontSize: '0.9rem',
                        },
                        align: "left",
                        data: obj.datas.map(x => x.name)
                    },
                    toolbox: {
                        show: false,
                        feature: {
                            mark: {
                                show: true
                            },
                            magicType: {
                                show: true,
                                type: ['pie']
                            },
                            restore: {
                                show: true
                            },
                            saveAsImage: {
                                show: true
                            }
                        }
                    },
                    series: [{
                        name: obj.seriesName,
                        type: 'pie',
                        avoidLabelOverlap: false,
                        radius: ['70%', '78%'],
                        data: obj.datas,
                        label: {
                            show: true,
                            position: 'center',
                            formatter: obj.labelFormatter,
                            fontSize: '0.9rem'
                        },
                        labelLayout: {
                            hideOverlap: false
                        },
                        labelLine: {
                            show: false
                        },
                        top: 0,
                        bottom: 30,
                        left: 0,
                        right: 200,
                        emphasis: {
                            label: {
                                show: false,
                                fontSize: '16',
                                fontWeight: 'bold'
                            }
                        }
                    }]
                };
            },
            initLineAndBarOption: function (echarts, obj, stackable, unit) {
                let result = {
                    title: {
                        text: !!obj.text ? obj.text : "",
                        textStyle: {
                            fontSize: '0.8rem',
                            fontWeight: "bold"
                        }
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'cross'
                        }
                    },
                    color: !!obj.color ? obj.color : [
                        "#1792E5",
                        "#7EB338",
                        "#FFC627",
                        "#F37021",
                        "#EE3124",
                        "#9B5986",
                        "#73bfdd",
                        "#5470c5"
                    ],
                    toolbox: {
                        show: false,
                        feature: {
                            restore: {
                                show: true
                            },
                            saveAsImage: {
                                show: true
                            }
                        }
                    },
                    legend: {
                        left: 'right',
                        itemWidth: 10,
                        itemHeight: 10,
                        data: !!obj.datas ? obj.datas.map(x => x.name) : []
                    },
                    grid: {
                        left: 20,
                        right: 20,
                        bottom: 10,
                        containLabel: true
                    },
                    yAxis: [{
                        name: `单位（${unit}）`,
                        type: 'value'
                    }],
                    xAxis: [{
                        type: 'category',
                        data: obj.xData,
                        axisTick: { // 轴标记
                            alignWithLabel: true
                        },
                        axisLabel: {
                            formatter: '{value} '
                        }
                    }],
                    series: obj.datas.map(x => {
                        let obj = {};
                        obj.name = x.name;
                        obj.type = x.type;
                        if (x.type === 'line') {
                            obj.smooth = true;
                        } else if (x.type === 'bar') {
                            obj.barMaxWidth = 8;
                            if (stackable) {
                                obj.stack = '0'
                            }
                        }
                        if (!!x.yAxisIndex) {
                            obj.yAxisIndex = x.yAxisIndex;
                        }
                        obj.data = x.datas;
                        return obj;
                    })
                };
                if (!!obj.formatter) {
                    result.tooltip.formatter = obj.formatter;
                }
                if (!!obj.yAxis) {
                    result.yAxis = obj.yAxis;
                }
                return result;
            },
            initBarOption: function (echarts, obj, stackable, unit, horizontal) {
                let xAxis, yAxis, result;
                if (!unit) {
                    unit = ''
                }
                if (!horizontal) {
                    xAxis = [{
                        type: 'category',
                        data: obj.xData,
                        axisTick: { // 轴标记
                            alignWithLabel: true
                        },
                        axisLabel: {
                            formatter: '{value} '
                        }
                    }];
                    yAxis = [{
                        type: 'value',
                        name: `单位（${unit}）`
                    }];
                } else {
                    xAxis = [{
                        show: false,
                        type: 'value',
                        name: `单位（${unit}）`,
                        max: 'dataMax'
                    }];
                    yAxis = [{
                        type: 'category',
                        data: obj.xData,
                        axisTick: { // 轴标记
                            alignWithLabel: true
                        },
                        axisLabel: {
                            formatter: '{value} '
                        }
                    }];
                }
                result = {
                    title: {
                        text: !!obj.text ? obj.text : "",
                        subtext: !!obj.subtext ? obj.subtext : "",
                        textStyle: {
                            fontSize: '0.8rem',
                            fontWeight: "bold"
                        }
                    },
                    tooltip: {
                        trigger: 'axis'
                        // axisPointer: _axisPointer
                    },
                    color: [
                        "#1792E5",
                        "#7EB338",
                        "#FFC627",
                        "#F37021",
                        "#EE3124",
                        "#9B5986",
                        "#73bfdd",
                        "#5470c5"
                    ],
                    toolbox: {
                        show: false,
                        feature: {
                            restore: {
                                show: true
                            },
                            saveAsImage: {
                                show: true
                            }
                        }
                    },
                    grid: {
                        left: horizontal ? 0 : 20,
                        right: horizontal ? 80 : 20,
                        bottom: 10,
                        top: horizontal ? 0 : 60,
                        containLabel: true
                    },
                    legend: {
                        left: 'right',
                        itemWidth: 10,
                        itemHeight: 10,
                        data: !!obj.datas ? obj.datas.map(x => x.name) : []
                    },
                    xAxis: xAxis,
                    yAxis: yAxis,
                    series: !!obj.datas ? obj.datas.map(x => {
                        x.type = 'bar';
                        if (stackable) {
                            x.stack = '0';
                        }
                        x.barMaxWidth = 8;
                        x.data = x.datas;
                        return x;
                    }) : []
                };
                if (!!obj.formatter) {
                    result.tooltip.formatter = obj.formatter;
                }
                return result;
            },
            initLineOption: function (echarts, obj, unit, showLabel) {
                if (!unit) {
                    unit = ''
                }
                let result = {
                    title: {
                        text: !!obj.text ? obj.text : "",
                        textStyle: {
                            fontSize: '0.8rem',
                            fontWeight: "bold"
                        }
                    },
                    tooltip: {
                        trigger: 'axis'
                        // axisPointer: _axisPointer
                    },
                    color: [
                        "#1792E5",
                        "#7EB338",
                        "#FFC627",
                        "#F37021",
                        "#EE3124",
                        "#9B5986",
                        "#73bfdd",
                        "#5470c5"
                    ],
                    legend: {
                        left: 'right',
                        itemWidth: 10,
                        itemHeight: 10,
                        data: !!obj.datas ? obj.datas.map(x => x.name) : []
                    },
                    grid: {
                        left: 20,
                        right: 20,
                        bottom: showLabel ? 50 : 10,
                        containLabel: true
                    },
                    toolbox: {
                        show: false,
                        feature: {
                            mark: {
                                show: true
                            },
                            magicType: {
                                show: true,
                                type: []
                            },
                            restore: {
                                show: true
                            },
                            saveAsImage: {
                                show: true
                            }
                        }
                    },
                    calculable: true,
                    xAxis: [{
                        type: 'category',
                        data: obj.xData
                    }],
                    yAxis: [{
                        name: `单位（${unit}）`,
                        type: 'value'
                    }],
                    dataZoom: [{
                        show: showLabel,
                        realtime: showLabel,
                        start: 0,
                        end: 100,
                        xAxisIndex: [0]
                    }],
                    series: !!obj.datas ? obj.datas.map(x => {
                        let temp = {}
                        temp.name = x.name
                        temp.type = 'line';
                        temp.data = x.datas;
                        temp.smooth = true;
                        return temp;
                    }) : []
                }
                if (!!obj.formatter) {
                    result.tooltip.formatter = obj.formatter;
                }
                return result;
            },
            initGaugeOption: function (echarts, obj) {
                return {
                    series: [{
                        type: 'gauge',
                        startAngle: 180,
                        endAngle: 0,
                        min: obj.min,
                        max: obj.max,
                        splitNumber: -1,
                        axisLine: {
                            lineStyle: {
                                width: 8,
                                color: [
                                    [0.33, '#F37021'],
                                    [0.66, '#FFC627'],
                                    [1, '#7EB338']
                                ]
                            }
                        },
                        pointer: {
                            icon: 'path://M12.8,0.7l12,40.1H0.7L12.8,0.7z',
                            length: '12%',
                            width: "10rem",
                            offsetCenter: [0, '-60%'],
                            itemStyle: {
                                color: 'auto'
                            }
                        },
                        axisTick: {
                            length: 12,
                            lineStyle: {
                                color: 'auto',
                                width: 2
                            }
                        },
                        splitLine: {
                            length: 20,
                            lineStyle: {
                                color: 'auto',
                                width: 5
                            }
                        },
                        axisLabel: {
                            color: '#464646',
                            fontSize: 20,
                            distance: -60,
                            formatter: obj.formatter
                        },
                        title: {
                            offsetCenter: [0, '50%'],
                            fontSize: 14
                        },
                        detail: {
                            fontSize: "2rem",
                            offsetCenter: [0, '0%'],
                            valueAnimation: true,
                            formatter: obj.detailFormatter,
                            color: 'auto'
                        },
                        data: obj.data
                    }]
                }
            },
            initSankeyOption: function (echarts, obj) {
                return {
                    tooltip: {
                        trigger: 'item',
                        triggerOn: 'mousemove'
                    },
                    series: [
                        {
                            type: 'sankey',
                            emphasis: {
                                focus: 'adjacency'
                            },
                            lineStyle: {
                                curveness: 0.5
                            },
                            data: obj.data,
                            links: obj.links
                        }
                    ]
                }
            }
        }
    };
    exports('layplugin', layplugin);
});